package allparser;

import java.io.*;

public class URandomAccessFile extends RandomAccessFile {
    boolean endianType = true;
    
    public URandomAccessFile(String name, String mode) throws FileNotFoundException {
        super(name, mode);
    }
    
    public URandomAccessFile(File file, String mode) throws FileNotFoundException {
        super(file, mode);
    }
    
    public URandomAccessFile(String name, String mode, char type) throws FileNotFoundException {
        super(name, mode);
        
        if(type=='l') endianType = false;
        else endianType = true;
    }
        
    public void setEndian(char type){
        if(type=='l') endianType = false;
        else endianType = true;
    }

    public char getEndian(){
        if(endianType) return 'b';
        else return 'l';
    }
    
    public int readSInt() throws IOException {
        int firstByte = this.readUnsignedByte();
        int secondByte = this.readUnsignedByte();
        int thirdByte = this.readUnsignedByte();
        int fourthByte = this.readUnsignedByte();
        
        if(endianType){
            return ((int) (firstByte << 24
	                | secondByte << 16
                        | thirdByte << 8
                        | fourthByte));
        }else{
            return ((int) (fourthByte << 24
	                | thirdByte << 16
                        | secondByte << 8
                        | firstByte));
        }
    }
    
    public long readUInt() throws IOException {
        int firstByte = this.readUnsignedByte();
        int secondByte = this.readUnsignedByte();
        int thirdByte = this.readUnsignedByte();
        int fourthByte = this.readUnsignedByte();
        
        if(endianType){
            return ((long) (firstByte << 24
	                | secondByte << 16
                        | thirdByte << 8
                        | fourthByte))
                       & 0xFFFFFFFFL;
        }else{
            return ((long) (fourthByte << 24
	                | thirdByte << 16
                        | secondByte << 8
                        | firstByte))
                       & 0xFFFFFFFFL;
        }
    }
    
    public short readSShort() throws IOException {
        int firstByte = this.readUnsignedByte();
        int secondByte = this.readUnsignedByte();
        
        if(endianType){
            return (short)(firstByte << 8 | secondByte);
        }else{
            return (short)(secondByte << 8 | firstByte);
        }
    }
    
    public int readUShort() throws IOException {
        int firstByte = (0x000000FF & ((int)this.readByte()));
        int secondByte = this.readUnsignedByte();

        if(endianType){
            return (int)(firstByte << 8 | secondByte);
        }else{
            return (int)(secondByte << 8 | firstByte);
        }
    }
    
    public byte readSByte() throws IOException {
        return this.readByte();
    }
    
    public short readUByte() throws IOException {
        int firstByte = this.readUnsignedByte();
	return (short)firstByte;
    } 
}
